package com.ruoyi.system.service.impl.fxImpl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.fx.FxOrder;
import com.ruoyi.system.domain.fx.FxOrderItem;
import com.ruoyi.system.domain.fx.FxSupplierGood;
import com.ruoyi.system.domain.fx.FxUser;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.mapper.fx.FxOrderItemMapper;
import com.ruoyi.system.mapper.fx.FxOrderMapper;
import com.ruoyi.system.mapper.fx.FxSupplierGoodMapper;
import com.ruoyi.system.mapper.fx.FxUserMapper;
import com.ruoyi.system.service.fx.IFxOrderItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 订单项目Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-23
 */
@Service
public class FxOrderItemServiceImpl implements IFxOrderItemService
{
    @Autowired
    private FxOrderItemMapper fxOrderItemMapper;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    private FxUserMapper fxUserMapper;
    @Autowired
    private FxOrderMapper fxOrderMapper;
    @Autowired
    private FxSupplierGoodMapper fxSupplierGoodMapper;

    /**
     * 查询订单项目
     *
     * @param id 订单项目主键
     * @return 订单项目
     */
    @Override
    public FxOrderItem selectFxOrderItemById(Long id)
    {
        return fxOrderItemMapper.selectFxOrderItemById(id);
    }

    /**
     * 查询订单项目列表
     *
     * @param fxOrderItem 订单项目
     * @return 订单项目
     */
    @Override
    public List<FxOrderItem> selectFxOrderItemList(FxOrderItem fxOrderItem)
    {
        Long userId = SecurityUtils.getUserId(); // 获取用户id
        List<Integer> role = sysUserRoleMapper.selectByUserId(userId); // 查询角色

        FxUser fxUser = new FxUser(); // 新建用户
        fxUser.setSysUserid(userId);
        List<FxUser> userList = fxUserMapper.selectFxUserList(fxUser); // 查询用户
        List<Long> goodIdList = new ArrayList<>();
        FxOrder fxOrder = new FxOrder(); // 订单对象1
        if (StringUtils.isEmpty(role) || !role.contains(1) && StringUtils.isNotEmpty(userList)) {
            Long roleType = userList.get(0).getRoleType();
            // 用户 角色类型 4 该用户是店铺
            if (roleType == 4) {
                fxOrder.setOrderType(2L); // 订单状态为2 是客户订单
                fxOrder.setStoreId(userList.get(0).getStoreId());
            } else if (roleType == 3) { // 用户是供货商
                fxOrder.setOrderType(1L); // 订单状态为1 是店铺订单
                FxSupplierGood fxSupplierGood = new FxSupplierGood();
                fxSupplierGood.setUserId(userId);
                // 查询 当前供应商的 全部商品 判断订单有无该商品
                List<FxSupplierGood> goodList = fxSupplierGoodMapper.selectFxSupplierGoodList(fxSupplierGood);
                goodIdList = goodList.stream().map(FxSupplierGood::getId).collect(Collectors.toList()); // 拿到全部商品id
            } else {
                fxOrder.setOrderType(2L); // 客户订单
                fxOrder.setUserId(userId); // 当前客户自己的订单
            }
        }
        fxOrder.setDelFlag(0L); // 0 存在的订单
        List<FxOrder> orderList = new ArrayList<>();
        String createTime = null; // 创建时间
        if (!ObjectUtils.isEmpty(fxOrder.getCreateTime())) { // 判断时间是否为空
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //格式化
            createTime = sdf.format(fxOrder.getCreateTime());
        }
        fxOrder.setTime(createTime); // 存入订单
        List<FxOrderItem> orderItemList = new ArrayList<>();
        FxOrder fxOrder1 = fxOrderMapper.selectFxOrderById(fxOrder.getSearch()); // 搜索查询订单对象
        if (StringUtils.isNotNull(fxOrder1)) {
            orderList.add(fxOrder1); // 有对象
        }else {
            orderList = fxOrderMapper.selectAllBySearchAndCreateTime(fxOrder); //查订单
        }
        if (StringUtils.isEmpty(orderList)) {
            return orderItemList;
        }
        List<String> orderIdList = orderList.stream().map(FxOrder::getId).distinct().collect(Collectors.toList());
        // 查看详情
        if (StringUtils.isNotNull(fxOrderItem.getOrderId())) {
            orderIdList.clear();
            orderIdList.add(fxOrderItem.getOrderId());
        }
        fxOrderItem.setOrderIds(orderIdList);
        fxOrderItem.setGoodIds(goodIdList.size() == 0 ? null : goodIdList);
        startPage();
        orderItemList = fxOrderItemMapper.selectItemListByOrderIdsAndGoodIds(fxOrderItem);
        return orderItemList;
    }

    @Override
    public List<FxOrderItem> selecOrderItemList(FxOrderItem fxOrderItem) {
        FxOrderItem item = new FxOrderItem();
        item.setOrderId(fxOrderItem.getOrderId());
        List<FxOrderItem> itemList = fxOrderItemMapper.selectFxOrderItemList(item);
        Set<Long> supplierIds = itemList.stream().map(FxOrderItem::getSupplierGoodId).collect(Collectors.toSet());
        List<FxSupplierGood> supplierGoodList = fxSupplierGoodMapper.selectFxSupplierGoodListByGoodIds(new ArrayList<>(supplierIds));
        Map<Long, FxSupplierGood> map = supplierGoodList.stream().collect(Collectors.toMap(FxSupplierGood::getId, supplier -> supplier));
        itemList.forEach(orderItem -> {
            if (map.containsKey(orderItem.getSupplierGoodId())){
                FxSupplierGood fxSupplierGood = map.get(orderItem.getSupplierGoodId());
                orderItem.setGoodImg(fxSupplierGood.getGoodImg());
                orderItem.setGoodTypeName(fxSupplierGood.getClassTwoName());
            }
        });
        return itemList;
    }

    /**
     * 新增订单项目
     *
     * @param fxOrderItem 订单项目
     * @return 结果
     */
    @Override
    public int insertFxOrderItem(FxOrderItem fxOrderItem)
    {
        fxOrderItem.setCreateTime(DateUtils.getNowDate());
        return fxOrderItemMapper.insertFxOrderItem(fxOrderItem);
    }

    /**
     * 修改订单项目
     *
     * @param fxOrderItem 订单项目
     * @return 结果
     */
    @Override
    public int updateFxOrderItem(FxOrderItem fxOrderItem)
    {
        fxOrderItem.setUpdateTime(DateUtils.getNowDate());
        return fxOrderItemMapper.updateFxOrderItem(fxOrderItem);
    }

    /**
     * 批量删除订单项目
     *
     * @param ids 需要删除的订单项目主键
     * @return 结果
     */
    @Override
    public int deleteFxOrderItemByIds(Long[] ids)
    {
        return fxOrderItemMapper.deleteFxOrderItemByIds(ids);
    }

    /**
     * 删除订单项目信息
     *
     * @param id 订单项目主键
     * @return 结果
     */
    @Override
    public int deleteFxOrderItemById(Long id)
    {
        return fxOrderItemMapper.deleteFxOrderItemById(id);
    }
}
